class Node {
    constructor(data) {
        this.data = data
        this.next = null
    }
}
class LinkNode{
    constructor() {
        this.head = null
        this.count = 0
    }
    push(data) {
        let node = new Node(data)
        if(!this.head) {
            // 头结点为空
            this.head = node
        } else {
            let current = this.head
            while(current.next) {
                current = current.next
            }
            current.next = node
        }
        this.count++
    }
    get(index) {
        if(index >= this.count) {
            return undefined
        } else {
            let current = this.head
            for(let i =0; i< index; i++) {
                current = current.next
            }
            return current.data
        }
    }
    set(index, data) {
        if(index >= this.count) {
            return false
        } else {
            let current = this.head
            for(let i =0; i< index; i++) {
                current = current.next
            }
            current.data = data
            return true
        }
    }
    remove(index) {
        if(index >= this.count) {
            return false
        } else {
            let current = this.head
            let previous = null
            for(let i =0; i< index; i++) {
                previous = current
                current = current.next
            }
            previous.next = current.next
            this.count--
        }
    }
    insert(index, data) {
        if(index >= this.count) {
            return false
        } else {
            let node = new Node(data)
            let current = this.head
            let previous = null
            for(let i =0; i< index; i++) {
                previous = current
                current = current.next
            }
            previous.next = node
            node.next = current
            this.count++
            return true
        }
    }
}
